/*
 * CMutilate.java
 * 
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 * 
 * This file is part of Expression program is undefined on line 8, column 40 in Templates/Licenses/license-gplv3.txt..
 * 
 * Expression program is undefined on line 10, column 19 in Templates/Licenses/license-gplv3.txt. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Expression program is undefined on line 15, column 19 in Templates/Licenses/license-gplv3.txt. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Expression program is undefined on line 21, column 30 in Templates/Licenses/license-gplv3.txt..  If not, see <http://www.gnu.org/licenses/>.
 */

package dpssystem;

import dpsstats.CDamageStats;
import dpssystem.CAttackChance.HitType;
import dpssystem.CProc.AttackType;
import dpssystem.CWeapon.WeaponType;

/**
 *
 * @author Mani
 */
public class CMutilate extends CAbility {

    CDamageStats    m_damageStats;

    double m_bonusDamage = 181.0; 
    boolean m_available = false;
    double m_puncturingWounds;
    double m_findWeakness;
    double m_lethality;

    CMutilate (CSim p_sim)
    {
        super(p_sim, false);

        m_energyRequired = 60;
        if (p_sim.m_dpsInterface.m_glyphs.m_mutilate)
        {
            // Mutilate glyph remove 5 energy from requirement
            m_energyRequired -= 5;
        }

        // Both main hand and off hand need to be daggers plus the mutilate talent
        // needs to be specced to be able to even start considering mutilate ability.
        if (p_sim.m_player.m_MHWeapon.m_isDagger &&
            p_sim.m_player.m_OHWeapon.m_isDagger &&
            p_sim.m_dpsInterface.m_talents.getMutilate() != 0)
        {
            m_available = true;
        }

        m_damageStats = m_sim.m_stats.m_mutilateStats;
        m_puncturingWounds = m_sim.m_dpsInterface.m_talents.getPuncturingWoundsDouble() * 5.0;
        m_lethality = m_sim.m_dpsInterface.m_talents.getLethalityDouble() * 6.0;
        m_findWeakness = m_sim.m_dpsInterface.m_talents.getFindWeaknessDouble() * 2.0;
    }
    
    public boolean canExecute()
    {
        if (!m_available)
        {
            // Weapons and talents don't allow mutilate
            return false;
        }

        if (m_sim.m_player.m_energyRegen.m_currentEnergy < m_energyRequired)
        {
            // Not enough energy
            return false;
        }

        if (m_sim.m_currentRunTime < m_sim.m_gcd.m_endOfGCD)
        {
            // Still on GCD
            return false;
        }

        return true;
    }

    public void execute()
    {
        // Two handed dagger attack
        m_sim.m_gcd.startGCD();
        m_sim.m_player.m_energyRegen.useAbilityEnergy(m_energyRequired);

        // Attack with main hand
        if (attackMob(m_sim.m_player.m_MHWeapon))
        {
            // If hit then attack with off hand weapon
            attackMob(m_sim.m_player.m_OHWeapon);
        }
    }

    private boolean attackMob(CWeapon p_weapon)
    {
        // Special attack type for main hand
        HitType l_hitType = m_sim.m_attackChance.getSpecialAttackHit(m_sim.m_baseStats.getHitChance(), m_sim.m_baseStats.getExpertiseDodge());
        if (l_hitType == HitType.NORMAL)
        {
            // See if we crit with 2nd roll
            l_hitType = m_sim.m_attackChance.getSpecialAttackCrit(m_sim.m_baseStats.getCritChance() + m_puncturingWounds);
        } else if (l_hitType == HitType.MISS || l_hitType == HitType.DODGE)
        {
            m_damageStats.addStats(l_hitType, 0.0);
            // Nothing to see, we missed
            return false;
        }

        double l_powerDamage = m_sim.m_baseStats.getAttackPower() / 14;
        double l_damage = (((p_weapon.m_avgDamage / p_weapon.m_weaponSpeed) + l_powerDamage + m_bonusDamage) * ((m_sim.m_target.isPoisoned() ? 1.2:1.0)))
                                            * (p_weapon.m_weaponSpeed) * p_weapon.m_handModifier;

        if (l_hitType == HitType.CRIT)
        {
            l_damage *= (2.0 + m_lethality);
            m_sim.m_player.m_energyRegen.focusedAttacks();
            m_sim.m_target.m_comboPoints.sealFate();
        }

        l_damage *= (1.0 + m_findWeakness);
        l_damage -= m_sim.m_arpReduction.getDamageReduction(l_damage, p_weapon.m_isMace);
        m_damageStats.addStats(l_hitType, l_damage);
        m_sim.m_target.m_comboPoints.addComboPoints(1);
        
        p_weapon.m_poison.procPoison(false);

        // Do enchant procs for white attack
        boolean l_procTypeEnchant = false;
        if (p_weapon.m_enchantProc != null)
        {
            l_procTypeEnchant = true;
        }

        if (l_hitType == HitType.NORMAL)
        {
            m_sim.m_trinketManager.executeProc(m_sim, AttackType.MeleeSpecial, false, p_weapon);
            if (l_procTypeEnchant)
            {
                p_weapon.m_enchantProc.executeProc(m_sim, AttackType.MeleeSpecial, false, p_weapon);
            }
        } else if (l_hitType == HitType.CRIT)
        {
            m_sim.m_trinketManager.executeProc(m_sim, AttackType.MeleeSpecial, true, p_weapon);
            if (l_procTypeEnchant)
            {
                p_weapon.m_enchantProc.executeProc(m_sim, AttackType.MeleeSpecial, true, p_weapon);
            }
        }

        return true;
    }
}
